package com.xy.work.template.base.config.swagger;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Component;
import springfox.documentation.builders.*;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.schema.ScalarType;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import java.util.ArrayList;
import java.util.List;
/**
 * @Description Swagger 接口配置
 * @Author 小叶
 * @Version 1.0
 **/
@Component
@Data
@EnableOpenApi
@Slf4j
public class SwaggerConfiguration {
    @Value("${server.port}")
    private String port;
    @Value("${server.servlet.context-path}")
    private String contextPath;
    /**
     * 这是App模块接口文档
     * @return
     */
    @Bean
    public Docket appServiceApiDocDome() {
        log.info("生存App模块文档中,访问网址：http://localhost:{}{}/swagger-ui/index.html",port,contextPath);
        return new Docket(DocumentationType.OAS_30)
                .groupName("这是App模块接口文档")
                .pathMapping("/")
                //定义是否开启Swagger，false是关闭，可以通过变量去控制，线上关闭
                .enable(true)
                //配置文档的元信息
                .apiInfo(apiInfo())
                .select()
                //设置扫描控制器包名
                .apis(RequestHandlerSelectors.basePackage("com.xy.work.template.app.controller"))
                //正则匹配请求路径，并分配到当前项目组
                .paths(PathSelectors.any())
                .build()
                // 新版SwaggerUI3.0
                .globalRequestParameters(globalRequestParameters())
                .globalResponses(HttpMethod.GET,getGlobalResponseMessage())
                .globalResponses(HttpMethod.POST,getGlobalResponseMessage());
    }
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("shop文档")
                .description("微服务接口文档")
                .contact(new Contact("小叶", "https://www.xy.com", "xxx.@126.com"))
                .version("v1.0.0")
                .build();
    }
    /**
     * 配置全局通用参数
     * @return
     */
    private List<RequestParameter> globalRequestParameters() {

        List<RequestParameter> parameters = new ArrayList<>();
        parameters.add(new RequestParameterBuilder()
                .name("token")
                .description("登录令牌")
                .in(ParameterType.HEADER)
                .query(q -> q.model(m -> m.scalarModel(ScalarType.STRING)))
                .required(false)
                .build());
        return parameters;

    }
    /**
     * 生成通用的响应信息
     */
    private List<Response> getGlobalResponseMessage() {

        List<Response> list = new ArrayList<>();
        list.add(new ResponseBuilder()
                .code("4xx")
                .description("请求错误，根据code和msg检查")
                .build());

        return list;
    }



}
